<template>
  <div>
    <div class="option">
      <el-button @click="info.isShow = true" type="primary" round plain
        >新增</el-button
      >
    </div>
    <el-table :data="specsList">
      <el-table-column prop="id" label="ID" min-width="80"></el-table-column>
      <el-table-column
        prop="specsname"
        label="规格名称"
        min-width="180"
      ></el-table-column>
      <el-table-column prop="attrs" label="规格属性" min-width="180">
        <template v-slot="props">
          <el-button
            type="primary"
            size="mini"
            round
            plain
            v-for="(item, index) in props.row.attrs"
            :key="index"
            >{{ item }}</el-button
          >
        </template>
      </el-table-column>
      <el-table-column prop="status" label="规格状态" min-width="180">
        <template v-slot="props">
          <el-button
            type="success"
            size="mini"
            round
            plain
            v-if="props.row.status === 1"
            >正常</el-button
          >
          <el-button type="danger" size="mini" round plain v-else
            >禁用</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="180">
        <template v-slot="props">
          <el-button @click="edit(props.row)" type="primary" size="mini" round plain>编辑</el-button>
          <el-button @click="del(props.row.id)" type="danger" size="mini" round plain>删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @current-change="pageChange"
      background
      :total="specsTotal"
      layout="prev,pager,next"
      :page-size="size"
    ></el-pagination>
    <!-- 调用对话框组件 -->
    <Dialog ref="dialog" :info="info" @specsUpdate="specsUpdateHandle" />
  </div>
</template>

<script>
// 导入对话框组件
import Dialog from "./Dialog";
// 导入vuex辅助函数
import { mapState, mapActions } from "vuex";
export default {
  components: {
    Dialog,
  },
  data() {
    return {
      info: {
        isShow: false,
      },
      // 页码
      page: 1,
      // 每页数据条数
      size: 1,
    };
  },
  computed: {
    ...mapState("specs", ["specsList", "specsTotal"]),
  },
  methods: {
    ...mapActions("specs", ["getSpecsListAction", "getSpecsTotalAction"]),
    // 删除
    del(id){
      this.$myConfirm(()=>{
        this.$axios.post('/specsdelete',{id}).then(res=>{
          if(res.code===200){
            this.$message.success(res.msg);
            // 更新数据
            if(this.size===1 || Math.ceil(this.specsTotal/this.size)===this.page&&this.specsTotal%this.size===1){
                this.page-=1;
                if(this.page<1){
                  this.page=1;
                }
            }
            this.specsUpdateHandle();
          }else{
            this.$message.error(res.msg);
          }
        });
      });
    },
    // 编辑按钮点击事件处理函数
    edit(row){
      // 1-显示对话框组件
      this.info.isShow=true;  
      // 2-将待编辑的规格信息传递给对话框组件
      this.$refs.dialog.setFormData(row);
    },
    // 自定义事件specsUpdate的处理函数
    specsUpdateHandle() {
      // 请求规格列表
      this.getSpecsListAction({ page: this.page, size: this.size });
      // 请求规格总数
      this.getSpecsTotalAction();
    },
    // 分页按钮点击事件处理函数
    pageChange(page) {
      // 1-更新分页页码
      this.page = page;
      // 2-根据最新的分页页码请求数据
      this.getSpecsListAction({ page: this.page, size: this.size });
    },
  },
  created() {
    this.specsUpdateHandle();
  },
};
</script>

<style>
</style>